//
//  FDAppController.m
//  foodash
//
//  Created by Kasper Kronborg on 11/02/09.
//  Copyright 2009 mdw.dk. All rights reserved.
//

#import "FDAppController.h"
#import "FDBrowserCell.h"
#import "FDColumnView.h"
#import "FDOioData.h"

@implementation FDAppController

- (id)init
{
	[super init];
	oioData = [[self regions] retain];
	[browser setCellClass:[FDBrowserCell class]];
	return self;
}

- (NSMutableArray *)regions
{
	FDOioData * data = [[FDOioData alloc] init];
	NSMutableArray * regions = [data regions];
	NSMutableArray * arr = [[[NSMutableArray alloc] init] autorelease];
	for (NSMutableDictionary * region in regions) {
		NSMutableDictionary * regionValue = [[[NSMutableDictionary alloc] init] autorelease];
		[regionValue setObject:[region objectForKey:@"navn"] forKey:@"title"];
		[regionValue setObject:[region objectForKey:@"nr"] forKey:@"id"];
		[regionValue setObject:[[NSArray alloc] init] forKey:@"contents"];
		[regionValue setObject:@"region" forKey:@"type"];
		[regionValue setObject:@"no" forKey:@"fetched"];
		[arr addObject:regionValue];
	}
	return arr;
}

- (NSMutableArray *)localCouncils:(int)region
{
	FDOioData * data = [[FDOioData alloc] init];
	NSMutableArray * councils = [data localCouncils:region];
	NSMutableArray * arr = [[[NSMutableArray alloc] init] autorelease];
	for (NSMutableDictionary * council in councils) {
		NSMutableDictionary * councilValue = [[[NSMutableDictionary alloc] init] autorelease];
		[councilValue setObject:[council objectForKey:@"navn"] forKey:@"title"];
		[councilValue setObject:[[NSArray alloc] init] forKey:@"contents"];
		[councilValue setObject:@"council" forKey:@"type"];
		[councilValue setObject:@"no" forKey:@"fetched"];
		[arr addObject:councilValue];
	}
	return arr;
}

- (NSInteger)browser:(NSBrowser *)sender numberOfRowsInColumn:(NSInteger)column
{
	if ([browser selectedCell] == nil) {
		NSLog(@"Returning count for number of rows in oioData: %i", [oioData count]);
		return [oioData count] + 1;
	} else {
		NSMutableDictionary * obj = [[browser selectedCell] representedObject];
		NSMutableArray * contents = nil;
		if ([obj objectForKey:@"fetched"] == @"no") {
			int region = [[obj objectForKey:@"id"] intValue];
			contents = [self localCouncils:region];
			[obj setObject:@"yes" forKey:@"fetched"];
			[obj setObject:contents forKey:@"contents"];
		} else {
			contents = [obj objectForKey:@"contents"];
		}
		NSLog(@"Returning count for number of rows: %i", [contents count]);
		return [contents count] + 1;
	}
}

- (void)browser:(NSBrowser *)sender willDisplayCell:(id)cell atRow:(NSInteger)row column:(NSInteger)column
{
	row = row - 1;
	NSLog(@"Frame %f", [browser frameOfColumn:column].origin.y);
	if (row == -1) {
		[cell setCellType:@"foo"];
		NSString * food = @"fod ougr rwebuogrwh grwbouw hwbiuweh wrbuiwhi gwbui wgi hgwubgi hgrbugehi de";
		[food drawInRect:[browser frameOfColumn:column] withAttributes:nil];
		[cell setLeaf:YES];
		[cell setEnabled:NO];
		NSLog(@"Width: %f", [cell cellSize].width);
		NSLog(@"Creating view");
		FDColumnView * ourView = [[FDColumnView alloc] init];
		[sender addSubview:ourView];
		NSLog(@"Created view %@", ourView);
	} else {
		[cell setCellType:@"bar"];
		NSMutableArray * data = nil;
		if ([browser selectedCell] == nil) {
			data = oioData;
		} else {
			data = [[[browser selectedCell] representedObject] objectForKey:@"contents"];
		}
		[cell setStringValue:[[data objectAtIndex:row] objectForKey:@"title"]];
		[cell setRepresentedObject:[data objectAtIndex:row]];
	}
}

- (IBAction)rowSelected:(id)sender
{
//	NSLog(@"col %@", [browser selectedCell]);
//	NSLog(@"selected %@", [sender selectedRowInColumn:[sender selectedColumn]]);
/*	NSBrowserCell * cell = [browser selectedCellInColumn:0];
	NSLog(@"represented %@", [cell representedObject]);*/
}
@end
